// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');

//ネットワークチェック
if(Titanium.Network.online == false){
	alert('オフラインなのでデータ取得できません');
}else{
	var xmlUrl = 'http://webgrid.web.fc2.com/test/user.xml';

	//HTTPオブジェクト作成
	var xhr = Titanium.Network.createHTTPClient();
	xhr.open("GET",xmlUrl,false);

	//xmlを読み込んだ処理
	xhr.onload = function(){
		//XML取得
		var xmlDocument = Ti.XML.parseString(xhr.responseText);
		//<tab>ノードを取得
		var nodeList = xmlDocument.getElementsByTagName('tab');
		//TabGroup生成
		var tabGroup = Titanium.UI.createTabGroup();
		for(var i = 0;i < nodeList.length;i++){
			var node = nodeList.item(i);
			//<title>
			var tabTitle = node.getElementsByTagName('title').item(0);
			tabTitle = tabTitle.text;
			//<url>
			var viewUrl = node.getElementsByTagName('url').item(0);
			viewUrl = viewUrl.text;
			
			if(tabTitle != null && viewUrl != null && tabTitle != '' && viewUrl != ''){
				switch (tabTitle){
					//twitter
					case 'twitter':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					url:'tweetTab.js'
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					icon:'images/tabIcon/twitter.jpg',
	    					backgroundColor:'#000',
	    					window:win
						});
						//URLをセット
						win.tweetUrl = viewUrl;
						//tab登録
						tabGroup.addTab(tab);
						break;
						
					//facebook
				  	case 'facebook':
				  		//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					window:win
						});
						tabGroup.addTab(tab);
						break;
						
					//ameba
					case 'ameba':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					url:'amebaTab.js'
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					icon:'images/tabIcon/ameba.gif',
	    					backgroundColor:'#000',
	    					window:win
						});
						//URLをセット
						win.amebaUrl = viewUrl;
						win.tab = tab;
						//tab登録
						tabGroup.addTab(tab);
						break;
					
					//FC2
					case 'FC2':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					url:'amebaTab.js'
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					window:win
						});
						//URLをセット
						win.amebaUrl = viewUrl;
						win.tab = tab;
						//tab登録
						tabGroup.addTab(tab);
						break;
					
					//mixi
					case 'mixi':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					window:win
						});
						tabGroup.addTab(tab);
						break;
					
					//HP
					case 'HP':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					window:win
						});
						tabGroup.addTab(tab);
						break;
					
					//other
					case 'other':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					window:win
						});
						tabGroup.addTab(tab);
						break;
				}
				
			}
			 
		}
		// open tab group
		tabGroup.open();
	}
	//リクエスト送信
	xhr.send();
}
